# 字典 Dictionary

dic = {"name": "leslie", "age": 19, "height": 1.8}
print(type(dic))  # <class 'dict'>
print(dic)  # {'name': 'leslie', 'age': 19, 'height': 1.8}

# 查询 - 1: dic[keyName] 🏀🏀
print(dic["name"])  # leslie
# 查询 - 2: dic.get(keyName) 🏀🏀
print(dic.get("name"))  # leslie
# 查询 - 3: dic.get(keyName, defaultValue) 🏀🏀
print(dic.get("name", "default"))  # leslie
print(dic.get("name1", "leslie 1"))  # default
print(type(dic.get("name3")))  # <class 'NoneType'>

# 修改 通过key来找到 🏀🏀, 存在就更新, 不存在就添加
dic["weight"] = 50
print(dic)  # {'name': 'leslie', 'age': 19, 'height': 1.8, 'weight': 50}


# 删除 - 删除整个对象 🏀🏀
dic2 = {"name": "leslie", "age": 19, "height": 1.8}
print(dic2)  # {'name': 'leslie', 'age': 19, 'height': 1.8}
if "name" in dic2:
    del dic2
    print("Dict deleted")
else:
    print("Name key not found")


# 删除 - 删除属性 🏀🏀 不指定会报错
dic3 = {"name": "leslie", "age": 19, "height": 1.8}
print(dic3)  # {'name': 'leslie', 'age': 19, 'height': 1.8}
del dic3["name"]
print(dic3)  # {'age': 19, 'height': 1.8}

# 删除 - clear 🏀🏀
dic4 = {"name": "leslie", "age": 19, "height": 1.8}
dic4.clear()
print(dic4)  # {}


# 删除- pop, 必须指定属性
dic5 = {"name": "leslie", "age": 19, "height": 1.8}
dic5.pop("name")
# dic5.pop("name2") # Error
print(dic5)  # {'age': 19, 'height': 1.8}

# 删除 popItem 删除最后一个,3.7 之前随机删除, 3.7之后删除最后一个
dic6 = {"name": "leslie", "age": 19, "height": 1.8}
dic6.popitem()
print(dic6)  # {'name': 'leslie', 'age': 19}
